package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.authentication.PermissionAuth;
import com.woniuxy.entitys.TbCustomer;
import com.woniuxy.entitys.TbFix;
import com.woniuxy.services.FixService;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
public class FixController {
    @Autowired
    private FixService fixService;

    @PostMapping("fixList")
    @PermissionAuth("selectFix")
    public ResultVO getFixList(@RequestBody Map<String,Object> map){
        /*
         pageNum: this.pageInfo.pageNum,
        pageSize: this.pageInfo.pageSize,
        hid: this.hid,
        did: this.did,
        uid: this.uid,
        softdel: this.softdel,
        beginTmie:this.beginTime,
        endTime:this.endTime,
         */
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Integer hid = (Integer) map.get("hid");
        Integer did = (Integer) map.get("did");
        Integer uid = (Integer) map.get("uid");
        Integer softdel = (Integer) map.get("softdel");
        String beginTimeStr = (String) map.get("beginTmie");
        String endTimeStr = (String) map.get("endTime");
        Date beginTime = new Date();
        Date endTime = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if(beginTimeStr!="" && endTimeStr!=""){
            try {
                beginTime = simpleDateFormat.parse(beginTimeStr);
                endTime = simpleDateFormat.parse(endTimeStr);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            PageInfo<TbFix> pageInfo = fixService.getFixListWithTime(pageNum,pageSize,hid,did,uid,softdel,beginTime,endTime);
            return pageInfo!=null?ResultVO.success("success",pageInfo):ResultVO.failure("failed");
        }
        PageInfo<TbFix> pageInfo = fixService.getFixList(pageNum,pageSize,hid,did,uid,softdel);
        return pageInfo!=null?ResultVO.success("success",pageInfo):ResultVO.failure("failed");
    }

    @GetMapping("fixDetail/{fid}")
    public ResultVO getFixDetailByFid(@PathVariable("fid") Integer fid){
        TbFix tbFix =fixService.getFixDetailByFid(fid);
        return  tbFix!=null?ResultVO.success("success",tbFix):ResultVO.failure("failed");
    }

    /**
     * 新增维修记录
     */
    @PostMapping("/fix/add")
    @PermissionAuth("dmlFix")
    public ResultVO saveNewFix(@RequestBody Map<String,Object> map){
        /**
         * addHid: this.addValue[0],
         *         addUid:this.fixAddForm.addUid,
         *         addBid:this.fixAddForm,addBid,
         *         addMid:this.fixAddForm.addMid
         *         addSoftdel:this.fixAddForm.addSoftdel,
         */
        Integer hid = (Integer) map.get("addHid");
        Integer uid = (Integer) map.get("addUid");
        Integer bid = (Integer) map.get("addBid");
        Integer mid = (Integer) map.get("addMid");
        Integer softdel = (Integer) map.get("addSoftdel");//softdel:0已修复，1无法修复

        /**
         * 若前端传来的softdel为0（已修复）,则增加一条维修记录后同时修改床的状态为空闲0，故障记录状态为已修复2
         * 若前端传来的softdel为1（无法修复）,则增加一条维修记录后同时修改故障状态为无法修复2
         */

        TbFix tbFix = new TbFix();
        tbFix.setHid(hid);
        tbFix.setUid(uid);
        tbFix.setMid(mid);
        tbFix.setBid(bid);
        tbFix.setFixTime(new Date());
        tbFix.setSoftdel(0);
        Integer i = fixService.saveFix(tbFix);
        return i>0?ResultVO.success("success"):ResultVO.failure("failed");
    }

    /**
     * 修改fix时，先根据fid查询
     */
    @GetMapping("fix/one/{fid}")
    public ResultVO getFixByFid(@PathVariable("fid") Integer fid){
        TbFix tbFix = fixService.getFixByFid(fid);
        return tbFix!=null?ResultVO.success("success",tbFix):ResultVO.failure("failed");
    }

    /**
     * 修改fix
     */
    @PostMapping("/fix/update")
    @PermissionAuth("dmlFix")
    public ResultVO saveUpdFix(@RequestBody Map<String,Object> map){
        /**
         *  updFid:this.fixUpdForm.fid,
         *         updHid: this.updValue[0],
         *         updUid:this.fixUpdForm.updUid,
         *         updBid:this.fixUpdForm.updBid,
         *         updMid:this.fixUpdForm.updMid
         *         updSoftdel: this.fixUpdForm.updSoftdel,
         */
        Integer fid = (Integer) map.get("updFid");
        Integer hid = (Integer) map.get("updHid");
        Integer uid = (Integer) map.get("updUid");
        Integer bid = (Integer) map.get("updBid");
        Integer mid = (Integer) map.get("updMid");
        Integer softdel = (Integer) map.get("updSoftdel");
        TbFix tbFix = new TbFix();
        tbFix.setFid(fid);
        tbFix.setHid(hid);
        tbFix.setUid(uid);
        tbFix.setMid(mid);
        tbFix.setBid(bid);
//        tbFix.setFixTime(new Date());
        tbFix.setSoftdel(softdel);
        Integer i = fixService.saveUpdFix(tbFix);
        return i>0?ResultVO.success("success"):ResultVO.failure("failed");
    }
}
